#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
const int N = 360, M = 50;
int dp[M][M][M][M];
int x[N];
int cn[5];
int n, m;
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> x[i];
    }
    for (int j = 1; j <= m; j++)
    {
        int y = 0;
        cin >> y;
        cn[y]++;
    }
    dp[0][0][0][0] = x[1];
    for (int a = 0; a <= cn[1]; a++)
        for (int b = 0; b <= cn[2]; b++)
            for (int c = 0; c <= cn[3]; c++)
                for (int d = 0; d <= cn[4]; d++)
                {
                    int i = 1 + 1 * a + 2 * b + c * 3 + d * 4;
                    int& t = dp[a][b][c][d];
                    if (a)
                    {
                        t = max(t, dp[a - 1][b][c][d] + x[i]);
                    }
                    if (b)
                    {
                        t = max(t, dp[a][b - 1][c][d] + x[i]);
                    }
                    if (c)
                    {
                        t = max(t, dp[a][b][c - 1][d] + x[i]);
                    }
                    if (d)
                    {
                        t = max(t, dp[a][b][c][d - 1] + x[i]);
                    }
                }
    cout << dp[cn[1]][cn[2]][cn[3]][cn[4]];
    return 0;
}